package com.huawei.translate.notification.builders;

import com.huawei.translate.model.Application;
import com.huawei.translate.notification.EmailEntity;
import com.huawei.translate.notification.model.EmailTemplate;
import com.huawei.translate.notification.repository.EmailTemplateRepository;
import com.huawei.translate.notification.service.EmailTemplateType;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;


@Component
public class ApplicationRejectEmailBuilder implements EmailBuilder {

    private Logger logger = Logger.getLogger(ApplicationRejectEmailBuilder.class);

    private static EmailTemplateType templateName = EmailTemplateType.ApplicationRejected;

    private Application application;

    @Autowired
    private EmailTemplateRepository emailTemplateRepository;

    @Value("${sys.email}")
    private String sysEmails;

    @Override
    public EmailTemplateType getTemplateName() {
        return templateName;
    }

    @Override
    public EmailEntity build() {
        EmailEntity emailEntity = new EmailEntity();
        emailEntity.setSubject("未成功申请翻译服务");
        emailEntity.setFrom(sysEmails);
        emailEntity.setTo(application.getPoEmail());
        logger.info("Template Name is:" + templateName.name());
        List<EmailTemplate> templates = emailTemplateRepository.findByTemplateName(templateName.name());
        if (templates.size() < 1 || templates == null) {
            logger.info("Can not find ApplicationRejectEmail email template");
            emailEntity.setContent("");
            return emailEntity;
        }
        EmailTemplate template = templates.get(0);
        emailEntity.setContent(template.getTemplate());
        return emailEntity;
    }

    @Override
    public EmailBuilder WithApplication(Application application) {
        this.application = application;
        return this;
    }

    public Application getApplication() {
        return application;
    }

    public void setApplication(Application application) {
        this.application = application;
    }
}
